package com.css.fxfzdzzh.base.dict.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.base.dict.repository.entity.DictDirectoryEntity;
import com.css.fxfzdzzh.base.dict.service.DictDirectoryService;
import com.css.fxfzdzzh.base.dict.viewobjects.DictDirectoryQueryParams;
import com.css.fxfzdzzh.base.dict.viewobjects.DictDirectoryVO;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/test/dictDirectorys/")
@Slf4j
public class DictDirectoryController {
    @Autowired
    DictDirectoryService dictDirectoryService;

    @GetMapping("queryDictDirectorys")
    public RestResponse queryDictDirectorys(HttpServletRequest request, DictDirectoryQueryParams queryParams) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            jsonObject = dictDirectoryService.queryDictDirectorys(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            log.error("查询字典分类失败!",e);
            response=RestResponse.fail("查询字典分类失败!");
        }
        return response;
    }

    @GetMapping("initDictDirZtree")
    public RestResponse initDictDirZtree() {
        RestResponse response = null;
        try{
            List<DictDirectoryEntity> directoryEntityList = dictDirectoryService.initDictDirZtree();
            response = RestResponse.succeed(directoryEntityList);
        }catch (Exception e){
            log.error("获取字典分类树失败!",e);
            response=RestResponse.fail("获取字典分类树失败!");
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getDictDirectory(@PathVariable String id) {
        RestResponse response = null;
        DictDirectoryVO dde = null;
        try{
            dde = dictDirectoryService.getDictDirectory(id);
            response = RestResponse.succeed(dde);
        }catch (Exception e){
            log.error("获取字典分类失败!",e);
            response=RestResponse.fail("获取字典分类失败!");
        }
        return response;
    }
    @PostMapping
    public RestResponse saveDictDirectory(@RequestBody DictDirectoryEntity directoryEntity) {

        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictDirectoryService.saveDictDirectory(directoryEntity);
            json.put("message", "新增字典分类成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("新增字典分类失败!",e);
            json.put("message", "新增字典分类失败!");
            response=RestResponse.succeed(json);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateDictDirectory(@RequestBody DictDirectoryEntity directoryEntity)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictDirectoryService.updateDictDirectory(directoryEntity);
            json.put("message", "修改字典分类成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("修改字典分类失败!",e);
            json.put("message", "修改字典分类失败!");
            response=RestResponse.succeed(json);
        }
        return response;
    }
    @DeleteMapping("{ids}")
    public RestResponse deleteDictDirectorys(@PathVariable("ids") List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictDirectoryService.deleteDictDirectorys(ids);
            json.put("message", "删除字典分类成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("删除字典分类失败!",e);
            json.put("message", "删除字典分类失败!");
            response=RestResponse.succeed(json);
        }
        return response;
    }
    @GetMapping("validateDictDirCode")
    public RestResponse validateDictDirCode(@RequestParam String dictDirId, @RequestParam String dictDirCode) {
        RestResponse response = null;
        boolean isValid = false;
        try{
            isValid = dictDirectoryService.validateDictDirCode(dictDirId,dictDirCode);
            response = RestResponse.succeed(isValid);
        }catch (Exception e){
            log.error("校验字典分类编码唯一性失败!",e);
            response=RestResponse.fail("校验字典分类编码唯一性失败!");
        }
        return response;
    }
}